<template>
    <div>
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片视图区域 -->
        <el-card>
            <el-row>
                <el-col :span="4">
                    <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
                </el-col>
            </el-row>
            <!-- 表格 -->
            <tree-table class="treeTable"
                :data="cateList" 
                :columns="columns"
                :show-row-hover="false"
                stripe border
                :selection-type="false"
                :show-index="true"
                index-text="#"
                :expand-type="false"
            >
                <template slot="isok" slot-scope="scope">
                    <i class="el-icon-success" v-if="scope.row.cat_deleted === false" style="color: #019701;"></i>
                    <i class="el-icon-error" v-else style="color: red;"></i>
                </template>
                <template slot="order" slot-scope="scope">
                    <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
                    <el-tag size="mini" type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
                    <el-tag size="mini" type="warning" v-else>三级</el-tag>
                </template>
                <template slot="opt">
                    <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                    <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
                </template>
            </tree-table>
            <!-- 分页 -->
            <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum"
                :page-sizes="[2, 5, 10, 20]"
                :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
            </el-pagination>
        </el-card>

        <!-- 对话框 - 添加分类 -->
        <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close="addCateDialogClosed">
            <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px">
                <el-form-item label="分类名称：" prop="cat_name">
                    <el-input v-model="addCateForm.cat_name"></el-input>
                </el-form-item>
                <el-form-item label="父级分类：">
                    <!-- 
                        options：指定数据源
                        props：指定配置对象
                     -->
                    <el-cascader
                        v-model="selectedKeys"
                        :options="parentCateList"
                        :props="cascaderProps"
                        @change="parentCateChanged"
                        expand-trigger="hover"
                        clearable
                        change-on-select
                    ></el-cascader>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addCateDialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="addCate">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            // 商品分类的数据列表
            cateList: [],
            // 查询条件
            queryInfo: {
                type: 3,
                pagenum: 1,
                pagesize: 5
            },
            // 查询到的总条数
            total: 0,
            // 为 tree-table 指定列的定义
            columns: [
                {
                    label: '分类名称',
                    prop: 'cat_name'
                },
                {
                    label: '是否有效',
                    // 表示当前列类型为模板
                    type: 'template',
                    // 表示当前这一列的模板名称
                    template: 'isok'
                },
                {
                    label: '排序',
                    type: 'template',
                    template: 'order'
                },
                {
                    label: '操作',
                    type: 'template',
                    template: 'opt'
                }
            ],
            // 控制对话框-添加分类的显示与隐藏
            addCateDialogVisible: false,
            // 添加分类的表单数据
            addCateForm: {
                cat_pid: 0, // 父级分类的 id
                cat_name: '', // 分类的名称
                cat_level: 0 // 分类的等级，默认 1
            },
            // 添加分类表单数据的验证规则
            addCateFormRules: {
                cat_name: [
                    { required: true, message: '请输入分类名称', trigger: 'blur' }
                ]
            },
            // 父级列表
            parentCateList: [],
            cascaderProps: {
                label: 'cat_name',
                value: 'cat_id',
                children: 'children'
            },
            // 选中的父级分类的 id 数组
            selectedKeys: []
        }
    },
    created() {
        this.getCateList();
    },
    methods: {
        async getCateList() {
            const { data: res } = await this.$http.get('categories', { params: this.queryInfo });

            if(res.meta.status !== 200) {
                return this.$message.error(res.meta.msg);
            }

            this.$message.success(res.meta.msg);
            this.cateList = res.data.result;
            this.total = res.data.total;
        },
        // 监听要显示的每页条数是否发生改变
        handleSizeChange(pagesize) {
            this.queryInfo.pagesize = pagesize;
            this.getCateList();
        },
        // 监听当前页码是什么
        handleCurrentChange(pagenum) {
            this.queryInfo.pagenum = pagenum;
            this.getCateList();
        },
        // 显示对话框-添加分类
        showAddCateDialog() {
            this.getParentCateList();
            this.addCateDialogVisible = true;
        },
        // 监听对话框-添加分类的关闭事件
        addCateDialogClosed() {
            this.addCateForm.cat_name = '';
            this.addCateForm.cat_pid = 0;
            this.addCateForm.cat_level = 0;
            this.selectedKeys = [];
        },
        // 获取父级分类的数据列表
        async getParentCateList() {
            const { data: res } = await this.$http.get('categories', {
                params: { type: 2 }
            });
            console.log(res);
            if(res.meta.status !== 200) {
                return this.$message.error(res.meta.msg);
            }

            this.$message.success(res.meta.msg);
            this.parentCateList = res.data
        },
        // 监听值是否改变，设置分类信息
        parentCateChanged(val) {
            // 判断 val 是否有值，若没有，则表示顶层父级
            if(val.length > 0) {
                this.addCateForm.cat_pid = val[val.length - 1];
                this.addCateForm.cat_level = val.length;
            } else {
                this.addCateForm.cat_pid = 0;
                this.addCateForm.cat_level = 0;
            }
        },
        // 添加分类
        addCate() {
            this.$refs.addCateFormRef.validate(async valid => {
                if(!valid) return;
                
                const { data: res } = await this.$http.post('categories', this.addCateForm);
                
                if(res.meta.status !== 201) {
                    return this.$message.error(res.meta.msg);
                }

                this.$message.success(res.meta.msg);
                this.getCateList();
                this.addCateDialogVisible = false;
            });
        }
    }
}
</script>

<style lang="less">
.treeTable {
  margin-top: 15px;
}
.el-cascader-panel{
    height: 200px;
}
.el-cascader {
  width: 100%;
}
</style>